package com.codeoss.module.res.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.model.HierarchyData;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.module.ir.service.IIrMobileMenuService;
import com.codeoss.module.res.model.ResRelRoleMobileMenu;
import com.codeoss.module.res.request.RelRoleMMenuPageRequest;
import com.codeoss.module.res.response.RelRoleMMenuPageResponse;
import com.codeoss.module.res.service.IRelRoleMMenuPageService;
import com.codeoss.module.res.service.IResRelRoleMobileMenuService;

@Service
public class RelRoleMMenuPageService implements IRelRoleMMenuPageService {
    @Autowired
    private IIrMobileMenuService irMobileMenuService;
    @Autowired
    private IResRelRoleMobileMenuService resRelRoleMobileMenuService;
    @Autowired
	private DataSourceUtil dataSourceUtil;
	@Override
	public RelRoleMMenuPageResponse init(Long resRoleId) {
		// TODO Auto-generated method stub
		RelRoleMMenuPageResponse relRoleMMenuPageResponse=new RelRoleMMenuPageResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		List<Map<String, Object>> dataList= irMobileMenuService.listMaps();
		HierarchyData  menuData=dataSourceUtil.convertTableDataToHierarchyData(yunContext, dataList);
		relRoleMMenuPageResponse.setMenuData(menuData);
		
		List<Long> valueList=resRelRoleMobileMenuService.getMobileMenuIdListByRoleId(resRoleId);
		relRoleMMenuPageResponse.setValueList(valueList);
		
		return relRoleMMenuPageResponse;
	}
	@Override
	@Transactional
	public RelRoleMMenuPageResponse dataSave(RelRoleMMenuPageRequest request) {
		// TODO Auto-generated method stub
		RelRoleMMenuPageResponse relRoleMMenuPageResponse=new RelRoleMMenuPageResponse();
		QueryWrapper<ResRelRoleMobileMenu> queryWrapper=new QueryWrapper<ResRelRoleMobileMenu>();
		queryWrapper.eq("res_role_id", request.getResRoleId());
		resRelRoleMobileMenuService.remove(queryWrapper);
		List<Long> valueList=request.getValueList();
		if(valueList!=null) {
			for(int i=0;i<valueList.size();i++) {
				ResRelRoleMobileMenu resRelRoleMobileMenu=new ResRelRoleMobileMenu();
				resRelRoleMobileMenu.setResRoleId(request.getResRoleId());
				resRelRoleMobileMenu.setIrMobileMenuId(valueList.get(i));
				resRelRoleMobileMenuService.insert(resRelRoleMobileMenu);
			}
		}
		return relRoleMMenuPageResponse;
	}

}
